package com.sinopec.handdevice.view

import android.annotation.SuppressLint
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.base.BaseFragment
import com.sinopec.basemodule.model.*
import com.sinopec.basemodule.launchcontract.UHFInResultLaunch
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.resmodule.widget.SpaceItemDecoration
import com.sinopec.handdevice.R
import com.sinopec.handdevice.contract.StockInListContract
import com.sinopec.handdevice.presenter.StockInListPresenter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.fragment_stock_in_list.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: StockInListFragment
 * @Description:入库tab
 */
@Route(path = ARouterManagerUtils.GOTO_MAIN_STOCK_IN_FRAGMENT)
class StockInListFragment : BaseFragment<StockInListPresenter>(), StockInListContract.View {
    //列表适配器
    private var datas = arrayListOf<StockInListModel>()
    private var adapter: CommonAdapter<StockInListModel>? = null
    private var linearLayoutManager: LinearLayoutManager? = null

    /**
     * 入库扫码启动器
     */
    private val uhfInLauncher by lazy {
        registerForActivityResult(UHFInResultLaunch) {

        }
    }

    override fun initData() {
        super.initData()
    }

    override fun onResume() {
        super.onResume()
        srl.autoRefresh()
    }

    override fun initEvent() {
        super.initEvent()
        /**
         * 入库扫码启动器
         */
        uhfInLauncher
    }

    override fun initView(view: View) {
        mPresenter = StockInListPresenter(this)
        linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager
        rv_list.addItemDecoration(SpaceItemDecoration(10, 0))
        srl.setEnableLoadMore(false)
        //srl.autoRefresh()
        //刷新控件
        srl.setOnRefreshListener { refreshlayout: RefreshLayout ->
            mPresenter.stockInList("")
            refreshlayout.finishRefresh()
        }
        srl.setOnLoadMoreListener { refreshlayout: RefreshLayout ->
            mPresenter.stockInList("")
            refreshlayout.finishLoadMore()
        }
        adapter = object : CommonAdapter<StockInListModel>(mContext, R.layout.item_stock_in_list, datas) {
            @SuppressLint("SetTextI18n")
            override fun convert(
                holder: CustomViewHolder,
                model: StockInListModel,
                position: Int,
            ) {
                val itemView = holder.itemView
                //任务单号
                val tv_taskNo = holder.getView<TextView>(R.id.tv_taskNo)
                tv_taskNo.text = "任务单号：" + model.taskNo?.ifEmpty { "--" }
                //仓库名称-库位名称
                val tv_locationName = holder.getView<TextView>(R.id.tv_locationName)
                tv_locationName.text = "库位名称：" + model.locationName?.ifEmpty { "--" }
                //物料名称
                val tv_mtrlName = holder.getView<TextView>(R.id.tv_mtrlName)
                tv_mtrlName.text = "物料名称：" + model.mtrlName?.ifEmpty { "--" }
                //预计总重量（吨）
                val tv_expectedPalletQuantity = holder.getView<TextView>(R.id.tv_expectedPalletQuantity)
                tv_expectedPalletQuantity.text = "预计总重量（吨）：" + model.expectedPalletQuantity.toString()
                //任务优先级
                val tv_taskPriority = holder.getView<TextView>(R.id.tv_taskPriority)
                tv_taskPriority.text = "任务优先级：" + model.taskPriority.toString()

                /**
                 * 查看详情
                 */
                addDisposable(itemView.clicks()
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe {
                        ARouterManagerUtils.gotoStockInDetailActivity(this@StockInListFragment.mContext,
                            model)
                    })

                /**
                 * 读码
                 */
                val ll_scan = holder.getView<LinearLayout>(R.id.ll_scan)
                addDisposable(ll_scan.clicks()
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe {
                        uhfInLauncher.launch(model)
                    })
            }

        }
        rv_list.adapter = adapter
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_stock_in_list
    }

    override fun refreshUI(list: List<StockInListModel>) {
        datas.clear()
        datas.addAll(list)
        adapter?.notifyDataSetChanged()
    }
}